<template>
  <!-- 项目部下的岗位树结构组件 -->
  <div class="JobsTree-container">
      <a-tree
    v-if="!useSelect"
    :tree-data="treeData"
    v-on="$listeners"
    v-bind="$attrs"
    :replaceFields="replaceFields"
  />
  <a-tree-select :tree-data="treeData" style="width: 100%" :replaceFields="replaceFields" v-on="$listeners" v-bind="$attrs" :value="modelValue" @change="handleTreeSelectChanged" v-else />
  </div>
</template>

<script>
import { getProjectIdFromOrg, getChildBranch } from '@/api/index.js'
import { getJobs } from '@/api/subassembly/TreeSelectOrganization.js'
export default {
  model: {
    prop: 'modelValue',
    event: 'modelChange'
  },
  props: {
    modelValue: {
      type: [String, Number],
      default: ''
    },
    // 部门是否可选
    depSelectable: {
      type: Boolean,
      default: false,
    },
    // 岗位是否可选
    jobSelectable: {
      type: Boolean,
      default: true,
    },
    // 是否需要返回job的code代替id字段
    isJobCode: {
      type: Boolean,
      default: true
    },
    // 使用select-tree组件
    useSelect: {
      type: Boolean,
      default: false
    },
    // 岗位过滤函数
    filterJobFn: {
      type: Function,
      default: null
    }
  },
  data() {
    return {
      treeData: [],
      replaceFields: {
        title: 'name',
        key: 'id',
        value: 'id',
        children: 'children',
      },
    }
  },
  created() {
    this.getProjectIdFromOrg()
  },
  methods: {
    handleTreeSelectChanged(val) {
      this.$emit('modelChange', val)
    },
    async getProjectIdFromOrg() {
      try {
        const params = {
          code: 'PD', // PD=项目部
          pageNum: 1,
          pageSize: 99999,
          baseOrgType: 1
        }
        const res = await getProjectIdFromOrg(params)
        const rootList = (res.data.list || []).map((item) => {
          return {
            ...item,
            children: [],
            selectable: this.depSelectable,
            key: item.id,
          }
        })
        this.treeData = rootList
        rootList.forEach((item) => {
          this.getChildBranch(item.id, item)
        })
      } catch (err) {
        console.error(err)
      }
    },
    async getChildBranch(parentId, target) {
      try {
        const params = {
          id: parentId,
          type: 2, // 子级组织的类型   1 单位   2部门
        }
        const res = await getChildBranch(params)
        const childList = (res.data || []).map((item) => {
          return {
            ...item,
            children: [],
            selectable: this.depSelectable,
            key: item.id,
          }
        })
        target.children = childList
        childList.forEach((item) => {
          this.getJobs(item.id, item)
        })
        console.log(this.treeData)
      } catch (err) {
        console.error(err)
      }
    },
    async getJobs(orgId, target) {
      try {
        const params = {
          orgId,
        }
        const res = await getJobs(params)
        const jobsList = (res.data || []).map((item) => {
          return {
            ...item,
            selectable: this.jobSelectable,
            id: this.isJobCode ? item.code : item.id,
            key: this.isJobCode ? item.code : item.id,
            isLeaf: true
          }
        })
        target.children = this.filterJobFn ? this.filterJobFn(jobsList) : jobsList
      } catch (err) {
        console.error(err)
      }
    },
  },
}
</script>

<style>
</style>